﻿namespace WLFramework.Web.Binder.ValueProvider
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;

    public class HttpFileCollectionValueProvider : DictionaryValueProvider<HttpPostedFile>
    {
        public HttpFileCollectionValueProvider()
            : base(ToDictionary(HttpContext.Current.Request.Files))
        {
        }

        public HttpFileCollectionValueProvider(HttpFileCollection collection)
            : base(ToDictionary(collection))
        {
        }

        private static Dictionary<string, HttpPostedFile> ToDictionary(HttpFileCollection collection)
        {
            Shield.AssertNotNull(ref collection, "collection");

            Dictionary<string, HttpPostedFile> target = new Dictionary<string, HttpPostedFile>(StringComparer.OrdinalIgnoreCase);
            if (collection.Count == 0)
            {
                return target;
            }

            for (int i = 0; i < collection.Count; i++)
            {
                target.Add(
                    collection.GetKey(i),
                    collection.Get(i));
            }

            return target;
        }
    }

#if FW4
    public class HttpFileBaseCollectionValueProvider : DictionaryValueProvider<HttpPostedFileBase>
    {
        public HttpFileBaseCollectionValueProvider(HttpFileCollectionBase collection)
            : base(ToDictionary(collection))
        {
        }

        private static Dictionary<string, HttpPostedFileBase> ToDictionary(HttpFileCollectionBase collection)
        {
            Shield.AssertNotNull(ref collection, "collection");

            Dictionary<string, HttpPostedFileBase> target = new Dictionary<string, HttpPostedFileBase>(StringComparer.OrdinalIgnoreCase);
            if (collection.Count == 0)
            {
                return target;
            }

            for (int i = 0; i < collection.Count; i++)
            {
                target.Add(collection.GetKey(i), collection.Get(i));
            }

            return target;
        }

    }
#endif
}
